var payoutChart;

//算力图表
$(function() {
  var hashrateChart = echarts.init($("#hashrateChart").get(0));
  var option = {
    tooltip: {
      trigger: 'axis'
    },
    grid: {
      left: '75',
      right: '0',
      top: '20',
      bottom: '40'
    },
    legend: {
      icon: 'rect',
      data: ['当前有效算力', '平均有效算力'],
      bottom: 'bottom',
      textStyle: {
        fontSize: 12
      }
    },
    xAxis: {
      type: 'category',
      boundaryGap: true,
      data: [],
      axisTick: {
        show: false
      },
      axisLabel: {
        show: false,
        interval: 0
      },
      axisLine: {
        show: false
      }
    },
    yAxis: {
      type: 'value',
      position: 'left',
      animation: true,
      scale: true
    },
    series: [{
      name: '当前有效算力',
      color: '#1F77B4',
      type: 'line',
      itemStyle: {
        normal: {
          color: '#1F77B4'
        },
        emphasis: {
          color: '#1F77B4'
        }
      },
      data: []
    }, {
      name: '平均有效算力',
      color: '#FF7F0E',
      symbolSize: 5,
      type: 'line',
      itemStyle: {
        normal: {
          color: '#FF7F0E'
        },
        emphasis: {
          color: '#FF7F0E'
        }
      },
      data: []
    }]
  };
  $.get(base_url + "/api/history-hashrate/"+getUrlParams("minerId"), function(datas) {
    var ds = {
      curr: [],
      avg: [],
      label: []
    };
    for(var i = 0; i < datas.length; i++) {
      if (i % 6 == 0) {
        ds.curr.push((datas[i]['hr']/1000/1000).toFixed(2));
        ds.avg.push((datas[i]['avgHR']/1000/1000).toFixed(2));
        ds.label.push(dateFormat(datas[i].timestamp*1000, "yyyy-MM-dd hh:mm:ss"));
      }
    }
    hashrateChart.setOption(option);
    hashrateChart.setOption({
      xAxis: { data: ds.label },
      series: [
        { data: ds.curr },
        { data: ds.avg }
      ]
    });
  });
});
//份额图表
$(function() {
  var shareChart = echarts.init($("#shareChart").get(0));
  var option = {
    tooltip: {
      trigger: 'axis'
    },
    grid: {
      left: '75',
      right: '0',
      top: '20',
      bottom: '40'
    },
    legend: {
      icon: 'rect',
      data: ['有效算力', '无效算力'],
      bottom: 'bottom',
      textStyle: {
        fontSize: 12
      }
    },
    xAxis: {
      type: 'category',
      boundaryGap: true,
      data: [],
      axisTick: {
        show: false
      },
      axisLabel: {
        show: false,
        interval: 0
      },
      axisLine: {
        show: false
      }
    },
    yAxis: {
      type: 'value',
      position: 'left',
      animation: true,
      min: 0,
      scale: true
    },
    series: [{
      name: '有效算力',
      type: 'bar',
      data: [],
      itemStyle: {
        normal: {
          color: '#1F77B4'
        }
      }
    }, {
      name: '无效算力',
      type: 'bar',
      data: [],
      itemStyle: {
        normal: {
          color: '#FF7F0E'
        }
      }
    }]
  };
  var datas = [{ "time": 1488797661000, "HR_M_LONG": 132606.316, "SH_I_M": 24, "A_W": 140, "HR_M_SHORT": 124117.5, "SH_V_M": 16549 }, { "time": 1488801472000, "SH_I_M": 18, "SH_V_M": 16910 }, { "time": 1488805074000, "SH_V_M": 17044, "SH_I_M": 26 }, { "time": 1488808828000, "SH_V_M": 17763, "SH_I_M": 28 }, { "time": 1488812512000, "SH_I_M": 36, "SH_V_M": 17835 }, { "time": 1488816260000, "SH_I_M": 27, "SH_V_M": 17631 }, { "time": 1488819883000, "SH_I_M": 29, "SH_V_M": 17445 }, { "time": 1488824418000, "HR_M_SHORT": 39210, "SH_V_M": 5228, "HR_M_LONG": 131393.75, "SH_I_M": 5, "A_W": 43 }, { "time": 1488828604000, "SH_V_M": 17674, "SH_I_M": 29 }, { "time": 1488832007000, "SH_I_M": 27, "SH_V_M": 17229 }, { "time": 1488835545000, "SH_I_M": 30, "SH_V_M": 17073 }, { "time": 1488839191000, "SH_V_M": 17430, "SH_I_M": 24 }, { "time": 1488842649000, "SH_V_M": 17335, "SH_I_M": 26 }, { "time": 1488846254000, "SH_V_M": 17034, "SH_I_M": 26 }, { "time": 1488849836000, "HR_M_LONG": 130858.24, "SH_I_M": 24, "A_W": 138, "HR_M_SHORT": 126225, "SH_V_M": 16830 }, { "time": 1488853550000, "SH_I_M": 19, "SH_V_M": 17453 }, { "time": 1488857329000, "SH_V_M": 17884, "SH_I_M": 33 }, { "time": 1488860919000, "SH_V_M": 17571, "SH_I_M": 29 }, { "time": 1488864789000, "SH_I_M": 32, "SH_V_M": 17423 }, { "time": 1488868495000, "SH_I_M": 33, "SH_V_M": 17819 }, { "time": 1488872269000, "SH_I_M": 26, "SH_V_M": 17884 }, { "time": 1488875983000, "HR_M_SHORT": 134415, "SH_V_M": 17922, "HR_M_LONG": 130835.303, "SH_I_M": 16, "A_W": 141 }, { "time": 1488879851000, "SH_V_M": 18217, "SH_I_M": 24 }]
  var ds = {
    valid: [],
    invalid: [],
    label: []
  };
  datas.forEach(function(item) {
    ds.valid.push(item['SH_V_M']);
    ds.invalid.push(item['SH_I_M'])
    ds.label.push(item.time);
  });
  shareChart.setOption(option);
  shareChart.setOption({
    xAxis: {
      data: ds.label
    },
    series: [
      { data: ds.valid },
      { data: ds.invalid }
    ]
  });
});
//矿工数量图表
$(function() {
  var workersChart = echarts.init($("#workersChart").get(0));
  var option = {
    tooltip: {
      trigger: 'axis'
    },
    grid: {
      left: '75',
      right: '0',
      top: '20',
      bottom: '40'
    },
    legend: {
      icon: 'rect',
      data: ['在线矿工'],
      bottom: 'bottom',
      textStyle: {
        fontSize: 12
      }
    },
    xAxis: {
      type: 'category',
      boundaryGap: true,
      data: [],
      axisTick: {
        show: false
      },
      axisLabel: {
        show: false,
        interval: 0
      },
      axisLine: {
        show: false
      }
    },
    yAxis: {
      type: 'value',
      position: 'left',
      animation: true,
      scale: true,
      minInterval: 1,
      min: 0
    },
    series: [{
      name: '在线矿工',
      type: 'line',
      itemStyle: {
        normal: {
          color: '#1F77B4'
        },
        emphasis: {
          color: '#1F77B4'
        }
      },
      data: []
    }]
  };
  $.get(base_url + "/api/history-workers-online/"+getUrlParams("minerId"), function(data) {
    var ds = {
      workers: [],
      label: []
    };
    data.forEach(function(item, idx) {
      ds.workers.push(item['workers_online']);
      ds.label.push(dateFormat(item.timestamp*1000, "yyyy-MM-dd hh:mm:ss"));
    });
    workersChart.setOption(option);
    workersChart.setOption({
      xAxis: {
        data: ds.label
      },
      series: [
        { data: ds.workers }
      ]
    });
  });
});
$(function() {
  $("[data-toggle='tooltip']").tooltip();
  $(".miner-address span").text(getUrlParams("minerId"));
  queryMinerInfo(getUrlParams("minerId"));
  $('.miner-types a').click(function(e) {
    e.preventDefault()
    $(this).tab('show')
    $(this).addClass("active").siblings(".active").removeClass("active")
    if($(this).attr("id") == "payouts-tab") {
      $(".payments-table-panel").show();
      $(".works-tabel-panel").hide();
      setTimeout(function() {
        payoutChart.resize();
      }, 300);
    } else {
      $(".works-tabel-panel").show();
      $(".payments-table-panel").hide();
    }
  })
  
  $(".online-btn").click(function() {
    if ($(".online").size()) {
      $(".online").removeClass("none").siblings(".offline").addClass("none")
    } else {
      $(".offline").addClass("none");
    }
  });
  $(".offline-btn").click(function() {
    if ($(".offline").size()) {
      $(".offline").removeClass("none").siblings(".online").addClass("none")
    } else {
      $(".online").addClass("none");
    }
  });
});

function queryMinerInfo(minerId) {
  $.get(base_url + "/api/accounts/" + minerId, function(data) {
    $(".just_span").text(unitCover(data.currentHashrate));
    $(".pending-balance").text(data.stats.balance / 1000000000);
    $(".online-work").text(data.workersOnline);
    $(".active-work").children(":eq(0)").text(data.workersOnline)
    $(".active-work").children(":eq(1)").text(data.workersOffline)
    $(".offline-work").text(data.workersOffline);
    $(".24hours").text(data.stats.profitLastDay / 1000000000);
    showWorks(data.workers);
    showPayouts(data.payments);
  })
}

function showWorks(workers) {
  $.each(workers, function(key, item) {
    var $tr = $("<tr>").addClass(item.offline ?'offline' :'online');
    $("<td>").text(key).appendTo($tr);
    $("<td>").text(unitCover(item.hr)).appendTo($tr);
    $("<td>").text(unitCover(item.hr2)).appendTo($tr);
    $("<td>").text(dateFormat(item.lastBeat * 1000, "yyyy-MM-dd hh:mm:ss")).appendTo($tr);
    $tr.appendTo($(".miners"));
  })
  dt = $('#workers').DataTable({
    "paging": false,
    "info": false,
    'bSort': false,
    "stateSave": true,
    'language': {
      "search": "搜索:",
    }
  });
}

function showPayouts(payments) {
  var ds = {
    amounts: [],
    label: []
  };
  $.each(payments, function(index, item) {
    var $tr = $("<tr>");
    $("<td>").addClass("text-center").text(dateFormat(item.timestamp * 1000, "yyyy-MM-dd hh:mm:ss")).appendTo($tr);
    $("<td>").addClass("text-hidden").text(item.tx).appendTo($tr);
    $("<td>").text(item.amount / 1000000000).appendTo($tr);
    ds.amounts.push(item.amount / 1000000000);
    ds.label.push(dateFormat(item.timestamp * 1000, "yyyy-MM-dd"));
    $tr.appendTo($(".payments"));
  });
  showPayoutsChat(ds);
}

function showPayoutsChat(ds) {
  ds.amounts = ds.amounts.reverse();
  ds.label = ds.label.reverse();
  payoutChart = echarts.init($("#payoutChart").get(0));
  var option = {
    tooltip: {
      trigger: 'axis'
    },
    grid: {
      left: '75',
      right: '0',
      top: '20',
      bottom: '100'
    },
    legend: {
      icon: 'rect',
      data: ['ETH'],
      bottom: 'bottom'
    },
    xAxis: {
      type: 'category',
      boundaryGap: true,
      data: [],
      axisTick: {
        show: false
      },
      axisLabel: {
        show: true,
        interval: 0,
        rotate: 60
      },
      axisLine: {
        show: false
      }
    },
    yAxis: {
      type: 'value',
      position: 'left',
      animation: true,
      scale: true,
      splitNumber: 10
    },
    series: [{
      name: poolConfig[coinType].coinUnit,
      type: 'line',
      itemStyle: {
        normal: {
          color: '#1F77B4'
        },
        emphasis: {
          color: '#1F77B4'
        }
      },
      data: []
    }]
  };
  payoutChart.setOption(option);
  payoutChart.setOption({
    xAxis: {
      data: ds.label
    },
    series: [
      { data: ds.amounts }
    ]
  });
}